﻿using System.IO;
using System.Text;

namespace OConf.Common
{
    public class FileCore
    {
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        public static void CreateDirectory(string dirPath)
        {
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
        }

        /// <summary>
        /// 将内容保存到指定文件中
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileContent"></param>
        /// <returns></returns>
        public static void SaveFile(string filePath, string fileContent)
        {
            using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
                {
                    sw.Write(fileContent);
                    sw.Close();
                    fs.Close();
                }
            }
        }

        /// <summary>
        /// 删除指定的文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static void DeleteFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }
    }
}
